﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using TbShop.Entity;
using TbShop.Business.Components;

namespace TbShop.Web.Helpers
{
    public class AuthorizationHelper
    {
        /// <summary>
        /// 根据权限标识判断当前用户是否有权限，目前标识字段为Authority表的Code字段，对应Constants.AuthorityIdentity的字段
        /// </summary>
        /// <param name="authorityIdentity"></param>
        /// <returns>true-有权限 false-没有权限</returns>
        public static bool IsAuthorised(string authorityIdentity)
        {
            //return true;
            AuthorityBC authorityBC = new AuthorityBC();
            bool isAuthorised = false;
            var userId = SessionHelper.Get(StaticWords.SessionUserIdKey);
            if (userId != null)
            {
                isAuthorised = authorityBC.IsUserAuthority(userId.ToString(), authorityIdentity);
            }
            return isAuthorised;
            //return true;
        }

        /// <summary>
        /// 获得一个RedirectResult对象，指向权限错误提示页，并弹出错误消息，适用于需要返回ActionResult的Action
        /// </summary>
        /// <returns>跳转对象</returns>
        public static System.Web.Mvc.ActionResult AuthorityErrorRedirection()
        {
            return new System.Web.Mvc.RedirectResult("/Authority/AuthorityError");
        }
    }
}